﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class GIANGDAY_Form : Form
    {
        THAMSO thamso = new THAMSO();
        KHOA_Data khoa_Data = new KHOA_Data();
        LOPHOC lophoc = new LOPHOC();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        LOPHOC_Data lophoc_Data = new LOPHOC_Data();
        MONHOC_Data monhoc_Data = new MONHOC_Data();
        GIANGDAY_Data giangday_Data = new GIANGDAY_Data();

        GIANGDAY giangday = new GIANGDAY();

        public GIANGDAY_Form()
        {
            InitializeComponent();
        }

        private void GIANGDAY_Form_Load(object sender, EventArgs e)
        {
            txtHocKi.Text = "";
            txtLoaiGiangDay.Text = "";
            txtMaGiangDay.Text = "GD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtSoTietChuan.Text = "";

            cbGiaoVien.Items.Clear();
            cbLopHoc.Items.Clear();
            cbMonHoc.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
            lophoc_Data.lophoc_Combobox(cbLopHoc);
            monhoc_Data.monhoc_Combobox(cbMonHoc);

            cbKhoa.Items.Clear();
            khoa_Data.khoa_Combobox(cbKhoa);

        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            txtHocKi.Text = "";
            txtLoaiGiangDay.Text = "";
            txtMaGiangDay.Text = "GD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtSoTietChuan.Text = "";

            cbGiaoVien.Items.Clear();
            cbLopHoc.Items.Clear();
            cbMonHoc.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
            lophoc_Data.lophoc_Combobox(cbLopHoc);
            monhoc_Data.monhoc_Combobox(cbMonHoc);

            cbKhoa.Items.Clear();
            khoa_Data.khoa_Combobox(cbKhoa);

            giangday.hienthi_GiangDay(dtgGiangDay);
            lophoc.hienthi_LopHoc(dtgLopHoc);
        }



        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaGiangDay.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Giang Day");
                txtMaGiangDay.Focus();
                return;
            }
            else if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon khoa");
                return;
            }
         
            else if (txtHocKi.Text == "")
            {
                MessageBox.Show("Ban chua nhap hoc ki");
                txtHocKi.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (txtSoTietChuan.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtSoTietChuan.Focus();
                return;
            }
            else
            {
                try
                {
                    if (Convert.ToInt16(txtHocKi.Text) < 0 || Convert.ToInt16(txtHocKi.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung hoc ki");
                        txtHocKi.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu hoc ki \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtNamHoc.Text) < 0 || Convert.ToInt16(txtNamHoc.Text) > DateTime.Now.Year + 5)
                    {
                        MessageBox.Show("Ban chua nhap dung nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu nam hoc \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtSoTietChuan.Text) < 0 || Convert.ToInt16(txtSoTietChuan.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung so tiet chuan");
                        txtSoTietChuan.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung so tiet chuan \n" + ex.Message);
                }

                giangday.loaiGD = txtLoaiGiangDay.Text.ToString();
                giangday.maGD = txtMaGiangDay.Text.ToString();
                giangday.maLop = ((ListItem)(cbLopHoc.SelectedItem)).value;
                giangday.maMH = ((ListItem)(cbMonHoc.SelectedItem)).value;
                giangday.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                giangday.hocki = (int)Convert.ToInt16(txtHocKi.Text);
                giangday.namhoc = (int)Convert.ToInt16(txtNamHoc.Text);
                giangday.sotietchuan =(int) Convert.ToInt16(txtSoTietChuan.Text);

                giangday.them_GiangDay(giangday);
                giangday.hienthi_GiangDay(dtgGiangDay);

                txtHocKi.Text = "";
                txtLoaiGiangDay.Text = "";
                txtMaGiangDay.Text = "GD";
                txtNamHoc.Text = DateTime.Now.Year.ToString();
                txtSoTietChuan.Text = "";

                cbGiaoVien.Items.Clear();
                cbLopHoc.Items.Clear();
                cbMonHoc.Items.Clear();

                giaovien_Data.giaovien_Combobox(cbGiaoVien);
                lophoc_Data.lophoc_Combobox(cbLopHoc);
                monhoc_Data.monhoc_Combobox(cbMonHoc);

                cbKhoa.Items.Clear();
                khoa_Data.khoa_Combobox(cbKhoa);

                lophoc.hienthi_LopHoc(dtgLopHoc);
                giangday.hienthi_GiangDay(dtgGiangDay);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaGiangDay.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Giang Day");
                txtMaGiangDay.Focus();
                return;
            }
            else if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon khoa");
                return;
            }
            else if (txtLoaiGiangDay.Text == "")
            {
                MessageBox.Show("Ban chua nhap Loai Giang Day");
                txtLoaiGiangDay.Focus();
                return;
            }
            else if (txtHocKi.Text == "")
            {
                MessageBox.Show("Ban chua nhap hoc ki");
                txtHocKi.Focus();
                return;
            }
            else if (txtNamHoc.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtNamHoc.Focus();
                return;
            }
            else if (txtSoTietChuan.Text == "")
            {
                MessageBox.Show("Ban chua nhap nam hoc");
                txtSoTietChuan.Focus();
                return;
            }
            else
            {
                try
                {
                    if (Convert.ToInt16(txtHocKi.Text) < 0 || Convert.ToInt16(txtHocKi.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung hoc ki");
                        txtHocKi.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu hoc ki \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtNamHoc.Text) < 0 || Convert.ToInt16(txtNamHoc.Text) > DateTime.Now.Year + 5)
                    {
                        MessageBox.Show("Ban chua nhap dung nam hoc");
                        txtNamHoc.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung kieu nam hoc \n" + ex.Message);
                }

                try
                {
                    if (Convert.ToInt16(txtSoTietChuan.Text) < 0 || Convert.ToInt16(txtSoTietChuan.Text) > 20)
                    {
                        MessageBox.Show("Ban chua nhap dung so tiet chuan");
                        txtSoTietChuan.Focus();
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung so tiet chuan \n" + ex.Message);
                }

                giangday.loaiGD = txtLoaiGiangDay.Text.ToString();
                giangday.maGD = txtMaGiangDay.Text.ToString();
                giangday.maLop = ((ListItem)(cbLopHoc.SelectedItem)).value;
                giangday.maMH = ((ListItem)(cbMonHoc.SelectedItem)).value;
                giangday.maGV = ((ListItem)(cbGiaoVien.SelectedItem)).value;
                giangday.hocki = Convert.ToInt16(txtHocKi.Text);
                giangday.namhoc = Convert.ToInt16(txtNamHoc.Text);
                giangday.sotietchuan = Convert.ToInt16(txtSoTietChuan.Text);

                giangday.sua_GiangDay(giangday);
                giangday.hienthi_GiangDay(dtgGiangDay);

                txtHocKi.Text = "";
                txtLoaiGiangDay.Text = "";
                txtMaGiangDay.Text = "GD";
                txtNamHoc.Text = DateTime.Now.Year.ToString();
                txtSoTietChuan.Text = "";

                cbGiaoVien.Items.Clear();
                cbLopHoc.Items.Clear();
                cbMonHoc.Items.Clear();

                giaovien_Data.giaovien_Combobox(cbGiaoVien);
                lophoc_Data.lophoc_Combobox(cbLopHoc);
                monhoc_Data.monhoc_Combobox(cbMonHoc);

                cbKhoa.Items.Clear();
                khoa_Data.khoa_Combobox(cbKhoa);

                lophoc.hienthi_LopHoc(dtgLopHoc);
                giangday.hienthi_GiangDay(dtgGiangDay);
            }
        }



        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaGiangDay.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ma Giang Day");
                txtMaGiangDay.Focus();
                return;
            }
            else
            {
                giangday.xoa_GiangDay(txtMaGiangDay.Text.ToString());
            }
            txtHocKi.Text = "";
            txtLoaiGiangDay.Text = "";
            txtMaGiangDay.Text = "GD";
            txtNamHoc.Text = DateTime.Now.Year.ToString() ;
            txtSoTietChuan.Text = "";

            cbGiaoVien.Items.Clear();
            cbLopHoc.Items.Clear();
            cbMonHoc.Items.Clear();

            giaovien_Data.giaovien_Combobox(cbGiaoVien);
            lophoc_Data.lophoc_Combobox(cbLopHoc);
            monhoc_Data.monhoc_Combobox(cbMonHoc);

            cbKhoa.Items.Clear();
            khoa_Data.khoa_Combobox(cbKhoa);

            lophoc.hienthi_LopHoc(dtgLopHoc);
            giangday.hienthi_GiangDay(dtgGiangDay);
        }

        private void dtgGiangDay_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgGiangDay.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaGiangDay.Text = dtgGiangDay.Rows[dong].Cells[0].Value.ToString();
            txtHocKi.Text = dtgGiangDay.Rows[dong].Cells[4].Value.ToString();
            txtNamHoc.Text = dtgGiangDay.Rows[dong].Cells[5].Value.ToString();
            txtLoaiGiangDay.Text = dtgGiangDay.Rows[dong].Cells[6].Value.ToString();
            txtSoTietChuan.Text = dtgGiangDay.Rows[dong].Cells[7].Value.ToString();

            String malophoc = dtgGiangDay.Rows[dong].Cells[1].Value.ToString();
            giangday_Data.layten_LopHoc(malophoc, cbLopHoc);
            String mamonhoc = dtgGiangDay.Rows[dong].Cells[2].Value.ToString();
            giangday_Data.layten_MonHoc(mamonhoc, cbMonHoc);
            String magiaovien = dtgGiangDay.Rows[dong].Cells[3].Value.ToString();
            giangday_Data.layten_GiaoVien(magiaovien, cbGiaoVien);
             
        }

        private void rdLyThuyet_CheckedChanged(object sender, EventArgs e)
        {
            if (cbMonHoc.Text == "")
            {
                MessageBox.Show("Ban chua chon mon hoc");
                return;
            }
            else if (rdLyThuyet.Checked)
            {
                float a = 0;
                a = monhoc_Data.tinchi_lythuyet(((ListItem)(cbMonHoc.SelectedItem)).value);
                txtSoTietChuan.Text = (a * thamso.tietchuan_tinchi()).ToString();
                txtLoaiGiangDay.Text = rdLyThuyet.Text;
            }
            else
            {
                float a = 0;
                a = monhoc_Data.tinchi_thuchanh(((ListItem)(cbMonHoc.SelectedItem)).value);
                txtSoTietChuan.Text = (a * thamso.tietchuan_tinchi()).ToString();
                txtLoaiGiangDay.Text = rdThucHanh.Text;
            }
        }

        private void cbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbLopHoc.Items.Clear();
            cbGiaoVien.Items.Clear();
            if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon khoa");
                return;
            }

            String makhoa = ((ListItem)(cbKhoa.SelectedItem)).value;

            giaovien_Data.giaovientheokhoa_Combobox(cbGiaoVien, makhoa);
            lophoc_Data.lophoctheokhoa_Combobox(cbLopHoc, makhoa);
        }

        private void cbMonHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbMonHoc.Text == "")
            {
                MessageBox.Show("Ban chua chon mon hoc");
                return;
            }
            else if (rdLyThuyet.Checked)
            {
                float a = 0;
                a = monhoc_Data.tinchi_lythuyet(((ListItem)(cbMonHoc.SelectedItem)).value);
                txtSoTietChuan.Text = (a * thamso.tietchuan_tinchi()).ToString();
                txtLoaiGiangDay.Text = rdLyThuyet.Text;
            }
            else
            {
                float a = 0;
                a = monhoc_Data.tinchi_thuchanh(((ListItem)(cbMonHoc.SelectedItem)).value);
                txtSoTietChuan.Text = (a * thamso.tietchuan_tinchi()).ToString();
                txtLoaiGiangDay.Text = rdThucHanh.Text;
            }
        }

        private void cbGiaoVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            String magiaovien = ((ListItem)(cbGiaoVien.SelectedItem)).value;
            giaovien_Data.thongtin_GiaoVien(dtgGiaoVien, magiaovien);
        }

        private void cbLopHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            String malophoc = ((ListItem)(cbLopHoc.SelectedItem)).value;
            lophoc_Data.hienthi_LopHoc(dtgLopHoc, malophoc);
        }

        private void txtMaGiangDay_MouseClick(object sender, MouseEventArgs e)
        {
            txtHocKi.Text = "";
            txtLoaiGiangDay.Text = "";
            txtMaGiangDay.Text = "GD";
            txtNamHoc.Text = DateTime.Now.Year.ToString();
            txtSoTietChuan.Text = "";
        }

        private void rdTenLop_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTLH.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenLop.Checked)
            {
                txtTGV.Visible = false;
                txtTLH.Visible = true;
                txtTLH.Text = "";
            }
        }

        private void rdTenGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTLH.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenLop.Checked)
            {
                txtTGV.Visible = false;
                txtTLH.Visible = true;
                txtTLH.Text = "";
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdTenLop.Checked)
            {
                giangday_Data.timkiemtheo_TenLop(dtgGiangDay, txtTLH.Text.ToString());
                txtTLH.Text = "";
            }
            else
            {
                giangday_Data.timkiemtheo_TenGiaoVien(dtgGiangDay, txtTGV.Text.ToString());
                txtTGV.Text = "";
            }
        }

       
    

        
    }
}
